package com.jxw.cidiansdkdemo;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jxw.cidiansdkdemo.util.MyUtils;

public class DicActivity extends AppCompatActivity implements JwarWrapper.OnVerify ,Constant {
    TextView dicName, searcr_result;
    EditText edit_word;
    int dic_type; //选择词典
    JwarWrapper mJwarWrapper;
    boolean isBind; //服务是否绑定成功
    //词典路径
    String path_xwcd = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/学王词典.JWDIC";
    String path_hzzd = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/汉字字典.JWDIC";
    String path_hycd = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/汉语词典.JWDIC";
    String path_cycd = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/成语词典.JWDIC";
    String path_gswcd = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/古诗文词典.JWDIC";
    String path_pyk = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ansystem/词典/拼音库.JWDIC";

    //词典是否打开
    boolean isOpen_xwcd,isOpen_hzzd,isOpen_hycd,isOpen_cycd,isOpen_gswcd,isOpen_pyk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dic);
        edit_word = findViewById(R.id.edit_word);
        dicName = findViewById(R.id.dicName);
        searcr_result = findViewById(R.id.searcr_result);

        dic_type = getIntent().getIntExtra("dic_type",0);
        mJwarWrapper = new JwarWrapper(this);
        mJwarWrapper.start(this);

        findViewById(R.id.del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_word.setText("");
            }
        });
        if(dic_type == TYPE_XWCD){
            findViewById(R.id.fuzzysearch).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final String keyWord = edit_word.getText().toString().replace(" ", "");
                    String wordobject = mJwarWrapper.matchWord(keyWord,20);
                    searcr_result.setText(wordobject);
                }
            });
        }else{
            findViewById(R.id.fuzzysearch).setVisibility(View.GONE);
        }

        //点击搜索查对应词典结果  各词典结果示例说明参考README文件
        findViewById(R.id.search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String keyWord = edit_word.getText().toString().replace(" ", "");
                if (dic_type == TYPE_XWCD) {
                    String wordobject = null;
                    if(MyUtils.isContainChinese(keyWord)){
                        wordobject = mJwarWrapper.inverseWord(keyWord);
                    }else{
                        wordobject = mJwarWrapper.searchWord(keyWord);
                    }
                    searcr_result.setText(wordobject);
                }else if (dic_type == TYPE_HZZD) {
                    String hzzdobject = mJwarWrapper.searchHYZD(keyWord);
                    searcr_result.setText(hzzdobject);
                }else if (dic_type == TYPE_HYCD) {
                    String hycdobject = mJwarWrapper.searchHYCD(keyWord);
                    searcr_result.setText(hycdobject);
                }else if (dic_type == TYPE_CYCD) {
                    String cyobject = mJwarWrapper.searchChengYu(keyWord);
                    searcr_result.setText(cyobject);
                }else if (dic_type == TYPE_GSWCD) {
                    String gswobject = mJwarWrapper.searchGSW(keyWord);
                    searcr_result.setText(gswobject);
                }else if (dic_type == TYPE_PYK) {
                    String pyobject = mJwarWrapper.searchPinYin(keyWord);
                    searcr_result.setText(pyobject);
                }
            }
        });
        dicName.setText(getResources().getStringArray(R.array.dic_name)[dic_type]);
        edit_word.setText(getResources().getStringArray(R.array.data_demo)[dic_type]);
    }

    @Override
    public void onError(boolean success) {
        Log.e("zzj", "onError:" + success);
        isBind = success;
        if(isBind){
            //打开指定词典数据
            if(dic_type == TYPE_XWCD) {
                isOpen_xwcd = mJwarWrapper.openXWCD(path_xwcd);
            }else if (dic_type == TYPE_HZZD) {
                isOpen_hzzd = mJwarWrapper.openHYZDFile(path_hzzd);
            }else if (dic_type == TYPE_HYCD) {
                isOpen_hycd = mJwarWrapper.openHYCDFile(path_hycd);
            }else if (dic_type == TYPE_CYCD) {
                isOpen_cycd = mJwarWrapper.openCYCDFile(path_cycd);
            }else if (dic_type == TYPE_GSWCD) {
                isOpen_gswcd = mJwarWrapper.openGSWCDFile(path_gswcd);
            }else if (dic_type == TYPE_PYK) {
                isOpen_pyk = mJwarWrapper.openPYFile(path_pyk);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //退出时关闭词典数据
        if (dic_type == TYPE_XWCD) {
            mJwarWrapper.closeXWCDFile();
        }else if (dic_type == TYPE_HZZD) {
            mJwarWrapper.closeHYZDFile();
        }else if (dic_type == TYPE_HYCD) {
            mJwarWrapper.closeHYCDFile();
        }else if (dic_type == TYPE_CYCD) {
            mJwarWrapper.closeCYCDFile();
        }else if (dic_type == TYPE_GSWCD) {
            mJwarWrapper.closeGSWCDFile();
        }else if (dic_type == TYPE_PYK) {
            mJwarWrapper.closePYFile();
        }
    }
}
